<?php

namespace App\Services;

// use App\Services\GetBytes;

class AGSignatureService {
    /**
     * 说明：获取签名【签名算法】
     *
     * @returnl
     *
     * @param type $data    = '{"a":"title"}';
     * @param type $api_url = "/user/saveouterrechargeitem";
     *
     * @author  daixinguo (daixinguo57@163.com)
     */
    public static function getSign($body, $api_url, $http_method, $security_key) {
        // $body_md5_base64 = base64_encode(GetBytes::toStr(unpack("c*", md5($body, true))));
        $body_md5_base64 = base64_encode(md5($body, true)); //也可以这样
        $content = $http_method . "\n" . $body_md5_base64 . "\n" . $api_url;
        $signature = base64_encode(hash_hmac("sha256", utf8_encode($content), utf8_encode($security_key), true));
        return $signature;
    }

    /**
     * 说明：验证签名【第三方投保接口】
     *
     * @param type
     *
     * @return
     * @author  daixinguo (daixinguo57@163.com)
     */
    public static function verifySign($request_sign, $body, $api_url, $http_method, $security_key) {
        $sign = self::getSign($body, $api_url, $http_method, $security_key);
        if ($request_sign != $sign) {
            return false;
        }
        return true;
    }
}
